﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace FrbaCommerce.Facturar_Publicaciones
{
    public partial class FormListadoFact : Form
    {
        
        private int user_id;



        public FormListadoFact(int user_id)
        {
            this.user_id = user_id;

            InitializeComponent();
        }


        private void cargarDatos()
        {
            dataGridView1.Columns.Clear();

            SqlConnection con = Program.obtenerConexion();

            SqlDataAdapter buscar = new SqlDataAdapter("SELECT fac_Nro AS 'Numero de Factura', fac_fecha AS 'Fecha', fac_total AS 'Total' "
                                        + " FROM Monkey_Coders.Facturas, Monkey_Coders.Publicaciones, Monkey_Coders.Item_factura"
                                        + " WHERE fac_forma_pago IS NULL"
                                        + " AND fac_usuario_id = " + user_id
                                        + " AND itfa_publicacion_id = pub_cod"
                                        + " AND itfa_factura_id = fac_Nro"
                                        + " AND pub_estado_id = 4"
                                        + " GROUP BY fac_Nro, fac_fecha, fac_total"
                                        + " ORDER BY fac_fecha", con);

            SqlCommandBuilder commandBuilder = new SqlCommandBuilder(buscar);
            DataSet ds = new DataSet();
            buscar.Fill(ds);
            dataGridView1.ReadOnly = true;
            dataGridView1.DataSource = ds.Tables[0];
        }

        private void Listado_Load(object sender, EventArgs e)
        {
            Generar_Publicacion.GenerarPublicacion publicacion = new Generar_Publicacion.GenerarPublicacion(user_id);
            comboBoxTipoDePago.SelectedIndex = 0;
            comboBoxCantFact.SelectedIndex = 0;
            cargarDatos();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void buttonRendir_Click(object sender, EventArgs e)
        {
            SqlConnection con = Program.obtenerConexion();
            
            SqlCommand actualizar = new SqlCommand("UPDATE Monkey_Coders.Facturas" 
            + " SET fac_forma_pago = '"+ comboBoxTipoDePago.Text +"'"
            + " WHERE fac_Nro IN" 
            + " (SELECT TOP ("+ Convert.ToInt32(comboBoxCantFact.Text) + ") fac_Nro"
            + " FROM Monkey_Coders.Facturas"
            + " WHERE fac_forma_pago IS NULL"
            + " AND fac_usuario_id = " + user_id
            + " ORDER BY fac_fecha)", con);

            actualizar.ExecuteNonQuery();

            MessageBox.Show("Facturas rendidas exitosamente!");
            cargarDatos();
        }

        private void buttonCerrar_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
